package com.ittao.taoaicodeai.ai.tools;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * MemoryId解析器
 * 用于解析包含appId和userId的复合memoryId
 */
@Slf4j
@Data
public class MemoryIdParser {
    
    private Long appId;
    private Long userId;
    
    /**
     * 解析memoryId
     * @param memoryId 复合memoryId，格式：appId:userId 或 appId
     * @return MemoryIdParser实例
     */
    public static MemoryIdParser parse(Object memoryId) {
        MemoryIdParser parser = new MemoryIdParser();
        
        if (memoryId == null) {
            log.warn("memoryId为空，无法解析");
            return parser;
        }
        
        String memoryIdStr = memoryId.toString();
        
        try {
            if (memoryIdStr.contains(":")) {
                // 复合ID格式：appId:userId
                String[] parts = memoryIdStr.split(":");
                if (parts.length == 2) {
                    parser.appId = Long.parseLong(parts[0]);
                    parser.userId = Long.parseLong(parts[1]);
                } else {
                    log.warn("复合memoryId格式错误: {}", memoryIdStr);
                }
            } else {
                // 简单ID格式：只有appId
                parser.appId = Long.parseLong(memoryIdStr);
                parser.userId = null;
            }
        } catch (NumberFormatException e) {
            log.error("解析memoryId失败: {}, 错误: {}", memoryIdStr, e.getMessage());
        }
        
        return parser;
    }
    
    /**
     * 获取appId
     * @return appId
     */
    public Long getAppId() {
        return appId;
    }
    
    /**
     * 获取userId
     * @return userId，可能为null
     */
    public Long getUserId() {
        return userId;
    }
    
    /**
     * 检查是否包含userId
     * @return true如果包含userId
     */
    public boolean hasUserId() {
        return userId != null;
    }
}